﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace Commission.Models
{
    public class Product
    {
        [Key]
        public int ProductId { get; set; }

        [Required]
        [MaxLength(10)]
        public string ProductNo { get; set; }

        [Required]
        public string ProductName { get; set; }

        [NotMapped]
        public string DisplayProduct {
            get
            {
                return this.ProductNo + " - " + this.ProductName;
            }
        }

        public int? FileId { get; set; }

        [ForeignKey("FileId")]
        public virtual FileUpload File { get; set; }

        public string FileUrl
        {
            get
            {
                if (File == null || File.File == null) return "";

                string imageBase64Data = Convert.ToBase64String(File.File);
                string imageDataURL = string.Format("data:image/png;base64,{0}", imageBase64Data);
                return imageDataURL;
            }
        }

        private string fileName;
        [NotMapped]
        public string FileName { 
            get 
            {
                return string.IsNullOrEmpty(fileName) 
                    ? (File != null ? File.FileName : "") : fileName;
            } 
            set 
            {
                fileName = value;
            }
        }

        [DisplayName("Select File to Upload")]
        [NotMapped]
        // mapped with image file choose on screen
        public HttpPostedFileBase ImageFile { get; set; }
    
        [Required]
        public decimal Price { get; set; }
        
        [Required]
        public int UnitId { get; set; }

        [ForeignKey("UnitId")]
        public virtual Unit Unit { get; set; }

        private string unitName;
        [NotMapped]
        public string UnitName
        {
            get
            {
                if (!string.IsNullOrEmpty(unitName))
                {
                    return unitName;
                }
                else if (this.Unit != null)
                {
                    return this.Unit.UnitName;
                }

                return "";
            }
            set
            {
                unitName = value;
            }
        }

        [Required]
        public int PPPoint { get; set; }

        [NotMapped]
        public int Quantity { get; set; }

        [NotMapped]
        public int Amount { get; set; }

        [NotMapped]
        public string DisplayAmount { get; set; }

        public string Note { get; set; }
    }
}